clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
* ---------------------------------------------- *
*compute aggregate LTG
use if LTG<. using $Work/ibes_ret, clear

gstats winsor LTG, cuts(1 99) by(datem) replace

bys datem: egen Tot=total(mcap)
gen vw=LTG*(mcap/Tot)

collapse (sum) LTG=vw (count) obs=vw, by(datem)
replace LTG=. if obs==0

tsset datem

gen LMLTG=L1.LTG
gen LQLTG=L3.LTG
gen dMoLLTG=LTG -LMLTG
gen dQrLLTG=LTG -LQLTG

forvalues x=1(1)5 { 
local t=12*`x'
gen  L`x'LTG=L`t'.LTG
gen  d`x'LLTG=LTG-L`x'LTG
}

foreach var in *LTG {
	rename `var' `var'agg
}

keep datem *agg
tempfile LTGagg
save "`LTGagg'", replace
* ************************************************
use permco permno datem LTG medest* *FE* cp* LTG *STG* EPS*Yr EPS mcap op inv BOOK INV PROF MOME SI* qruf1 cfacpr prc using $Work/EBR_new_monthly, clear

keep if qruf1<.
drop SIZE
rename SI10 SIZE

tsset permno datem
gen LMLTG=L1.LTG
gen LQLTG=L3.LTG
gen L1LTG=L12.LTG
gen L3LTG=L36.LTG
gen L5LTG=L60.LTG

gen LMSTG=L1.STG2
gen LQSTG=L3.STG2
gen L1STG=L12.STG2
gen L3STG=L36.STG2
gen L5STG=L60.STG2

gen dMoLLTG=LTG -L1.LTG
gen dQrLLTG=LTG -L3.LTG

gen dMoLSTG=STG2-L1.STG2
gen dQrLSTG=STG2-L3.STG2

tsset permno datem
forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTG=F`j'.LTG-LTG
	gen d`x'LLTG=LTG-L`j'.LTG
	gen d`x'LSTG=STG2-L`j'.STG2

}
gen LFE1=L12.FE1
gen LFE3=L36.FE3	
gen LFE5=L60.FE5
* ************************************************
gen lnLFE1=ln(EPS)/1-ln(L12.medest12)/1		if L12.medest12>0 
gen lnLFE3=ln(EPS)/3-ln(L36.medest36)/3		if L36.medest12>0 
gen lnLFE5=ln(EPS)/5-ln(L60.EPS)/5-L60.LTG	if L60.LTG>-1 

gstats winsor *FE* lnLF* cp* *LTG *STG*   op inv, cuts(1 99) by(datem) replace

tempfile June
save "`June'", replace
* ***************
eststo clear
* *******************
foreach p in BOOK SIZE INV PROF MOME  {
use if `p'<. using "`June'", clear
gen portf=1+int((`p'-1)/2)
gen d1LSTG2=STG2-L1STG2

collapse (mean)     *LTG STG2 *STG LFE1 LFE3 LFE5 lnLFE* op inv, by(datem portf)
/* ///////////////////////////////////////////////// */
* GENERATE HML SPREADS for variables other than returns
preserve
keep if portf==1 | portf==5  
foreach var of varlist *LTG STG2 *STG LFE1 LFE3 LFE5 lnLFE* op inv {
	replace `var'=-`var' if portf==5 & index("`p'","INV")+index("`p'","SIZE")==1
	replace `var'=-`var' if portf==1 & index("`p'","INV")+index("`p'","SIZE")==0
}	
foreach v of varlist  LTG L*LTG d*LTG STG2 L*STG d*STG LFE1 LFE3 LFE5 lnLFE* op inv {
bysort datem: egen count`v' = count(`v') 
}
collapse (sum)         LTG L*LTG d*LTG STG2 L*STG d*STG  lnLFE* op inv (mean) count*, by(datem)
foreach v of varlist   LTG L*LTG d*LTG STG2 L*STG d*STG  lnLFE* op inv  {
replace `v'=. if count`v'!=2
}

generate portf=6

tempfile PORTF
save "`PORTF'", replace

restore
append using "`PORTF'"
* ***************
merge 1:1 portf datem using  $Work/portfolio_`p'_qnic, keepusing(portf datem cum* L12ret lnDP L*ret*)
drop _merge
merge m:1 datem using "`LTGagg'"
drop if _merge==2
drop _merge
* ************************************************
* Run regressions for EBRs
* ************************************************
rename lnDP lndp

tsset portf datem
keep if L1LTG+d1LLTG+STG2+d1LLTGagg+L1LTGagg+lnLFE5<.
local Z "d*LLTG d*LSTG d*LLTG L*LTG L*STG lnLFE* *agg"   
foreach var of varlist Lret* lndp `Z' {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}
* *******************
generate lnpd=-lndp
generate DLT=dMoLLTG
generate DST=dMoLSTG
generate LLT=LMLTG
generate LST=LMSTG
generate LFE=1
generate DLTa=dMoLLTGagg
generate LLTa=LMLTGagg
	quietly: ivreg2 cumQRetMo1 lnpd 					if portf==6, bw(1 ) robust small
	scalar r2_1st= e(r2)
eststo: quietly: ivreg2 cumQRetMo1 lnpd DLT LLT DST LST LFE DLTa LLTa		if portf==6, bw(1 ) robust small
estadd scalar r2_1st

replace DLT=dQrLLTG
replace DST=dQrLSTG
replace LLT=LQLTG
replace LST=LQSTG
replace LFE=1
replace DLTa=dQrLLTGagg
replace LLTa=LQLTGagg
	quietly: ivreg2 cumQRetQr1 lnpd 					if portf==6, bw(4 ) robust small
	scalar r2_1st= e(r2)
eststo: quietly: ivreg2 cumQRetQr1 lnpd DLT LLT DST LST LFE DLTa LLTa		if portf==6, bw(4 ) robust small
estadd scalar r2_1st

replace DLT=d1LLTG
replace DST=d1LSTG
replace LLT=L1LTG
replace LST=L1STG
replace LFE=lnLFE1
replace DLTa=d1LLTGagg
replace LLTa=L1LTGagg	
	quietly: ivreg2 cumQRetYr1 lnpd 					if portf==6, bw(13) robust small
	scalar r2_1st= e(r2)
eststo: quietly: ivreg2 cumQRetYr1 lnpd  DLT LLT DST LST LFE DLTa LLTa		if portf==6, bw(13) robust small
estadd scalar r2_1st

replace DLT=d3LLTG
replace DST=d3LSTG
replace LLT=L3LTG
replace LST=L3STG
replace LFE=lnLFE3
replace DLTa=d3LLTGagg
replace LLTa=L3LTGagg
	quietly: ivreg2 cumQRetYr3 lnpd 					if portf==6, bw(37) robust small
	scalar r2_1st= e(r2)
eststo: quietly: ivreg2 cumQRetYr3 lnpd  DLT LLT DST LST LFE DLTa LLTa		if portf==6, bw(37) robust small
estadd scalar r2_1st
	
replace DLT=d5LLTG
replace DST=d5LSTG
replace LLT=L5LTG
replace LST=L5STG
replace LFE=lnLFE5
replace DLTa=d5LLTGagg
replace LLTa=L5LTGagg
	quietly: ivreg2 cumQRetYr5 lnpd						if portf==6, bw(61) robust small
	scalar r2_1st= e(r2)
eststo: quietly: ivreg2 cumQRetYr5 lnpd DLT LLT DST LST LFE DLTa LLTa		if portf==6, bw(61) robust small
estadd scalar r2_1st
}
esttab using $Tables/AppendixC4_June.csv	, nonotes  compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a r2_1st) replace
